//
//  AppDelegate.m
//  KaiChat
//
//  Created by lx on 2023/10/16.
//

#import "AppDelegate.h"
#import "HJStartPageController.h"
#import "HJNavigationController.h"
#import "HJMainController.h"
#import "HJLoginController.h"
#import "HJVersionUpdate.h"
#import "HJMessageImageDto.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self doInit];
    [self settingKeyboardManager];
    [self registeredNotification];
    
    NSString *res = safeDict([NSNumber new])[@"1"];
    NSLog(@"res = %@", safeDict([NSNumber new]));
    return YES;
}

- (void)doInit {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = UIColor.whiteColor;
    [self.window makeKeyAndVisible];
    [self gotoMainController];
}

- (void)gotoMainController {
    if (HJUserInfo.isLogin) {
        [self loginSucceed];
    } else {
        self.window.rootViewController = [[HJNavigationController alloc] initWithRootViewController:[[HJStartPageController alloc] init]];
    }
}

- (void)gotoLoginController {
    HJStartPageController *startPageController = [[HJStartPageController alloc] init];
    HJLoginController *loginController = [[HJLoginController alloc] init];
    HJNavigationController *nav = [[HJNavigationController alloc] initWithRootViewController:startPageController];
    nav.viewControllers = @[startPageController, loginController];
    self.window.rootViewController = nav;
}

- (void)registeredNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logOut) name:tokenErrorNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logOut) name:logoutNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSucceed) name:loginSucceedNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoLoginController) name:registerSucceedNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoLoginController) name:cancellationSucceedNotification object:nil];
}

- (void)loginSucceed {
//    [self registerJPush];
    self.window.rootViewController =  [[HJMainController alloc] init];
}

- (void)logOut {
    self.window.rootViewController = [[HJNavigationController alloc] initWithRootViewController:[[HJStartPageController alloc] init]];
    [HJUserInfo clear];
}

- (void)settingKeyboardManager {
    [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 20;
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [IQKeyboardManager sharedManager].previousNextDisplayMode = IQPreviousNextDisplayModeAlwaysHide;
    [IQKeyboardManager sharedManager].shouldShowToolbarPlaceholder = NO;
    [IQKeyboardManager sharedManager].enable = YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [HJVersionUpdate checkVersionUpdateInController:[HJTools currentViewController]];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
